let ajax = getApp().globalData.ajax;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordersList:[],
    skip: 0,
    take: 5,
    hasNext: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let json = {
      token: wx.getStorageSync('Cookie'),
      skip: this.data.skip,
      take: this.data.take,
    };
    this.getSystemuser(json);
  },

  //获取派单中心
  getSystemuser(json) {
    if (!this.data.hasNext) {
      ajax('order/receipt', json).then(res => {
        let ordersList = res.data||[];
        if (!ordersList.length) {
          this.setData({
            hasNext: true
          })
        }
        ordersList.forEach(item=>{
          item.create_time_res = item.create_time.toDateString();
        })

        ordersList = this.data.ordersList.concat(ordersList);
        this.setData({
          ordersList
        })
      })
    }
    
  },

  receiptClick(e){
    
    let order_no = e.currentTarget.dataset.orderno;
    let json = {
      token: wx.getStorageSync('Cookie'),
      order_no,
    }
    ajax('order/confirm', json).then(res => {
      if(res.code===0){
        wx.showToast({
          title: '接单成功',
          icon: 'success',
          image: '',
          duration: 1000,
          mask: true,
          success: function(res) {
            setTimeout(function(){
              wx.switchTab({
                url: '../recentchat/recentchat',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            },1000)
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let skip = this.data.skip;
    skip += this.data.take;
    this.setData({
      skip,
    })
    let json = {
      token: wx.getStorageSync('Cookie'),
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getSystemuser(json)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})